import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';
import { StatusFollowService } from '@core/statusFollow.service';
import { NzMessageService, UploadFile } from 'ng-zorro-antd';
import { ApiService } from '@core/api.service';
import { LocalStorage } from '@core/local.storage';
@Component({
    selector: 'app-splc-editor',
    templateUrl: './splc_editor.component.html',
    styleUrls: ['./splc_editor.component.less']
})

export class SplcEditorComponent implements OnInit {
    fileList: UploadFile[] = [];
    mode = '';
    gzxmList = [];
    qtbmNodes = [];
    phbmNodes = [];
    @Output() optSuccess = new EventEmitter();
    @Output() optCancel = new EventEmitter();
    wdlx: any = [];

    @Input() set params(param) {
        if (!param.mode) {
            return;
        }
        this.mode = param.mode;
        if (param.wdlx) {
            this.wdlx = param.wdlx;
        }
        if (param.mode === 'edit') {
            this.initSaveParam(param.row);
        } else {
            this.saveParam = {};
        }
        this.fileList = [];
    };
    saveParam: any = {};
    userInfo = JSON.parse(this.ls.get('loginInfo'));
    phbmList = [];
    constructor(
        private http: _HttpClient,
        public statusFollowService: StatusFollowService,
        public message: NzMessageService,
        public apiService: ApiService,
        private ls: LocalStorage,
    ) { }

    ngOnInit(): void {

    }

    initSaveParam(row) {
        this.saveParam = {};
        Object.assign(this.saveParam, row);
    }

    /**
     * 删除私立字段
     * @param param 1
     */
    removePrivateFields(param) {
        Object.keys(param).forEach(key => {
            if (!(key.indexOf('_')) || !param[key]) {
                delete param[key];
            }
        });
    }

    /**
     * 删除后台不接受的字段
     * @param param 1
     */
    removeUnacceptableFields(param) {
        Object.keys(param).forEach(key => {
            switch (key) {
                case 'checked':
                case 'sysDel':
                case 'sysDate':
                case 'sysUser':
                case 'status':
                    delete param[key];
                    break;
                default:
                    break;
            }
        });
    }


    beforeSave(row) {

        this.removeUnacceptableFields(row);
        this.removePrivateFields(row);
    }

    getSavableNo(row) {
        const indexMap = ['One', 'Two', 'Three', 'Four', 'Five']
        const nos = [];
        indexMap.forEach((el, i) => {
            if (!row[`doc${el}Url`]) {
                nos.push(i);
            }
        });
        return nos;
    }

    save() {
        const nos = this.getSavableNo(this.saveParam);
        const formData = new FormData();
        if (this.fileList.length > nos.length) {
            this.message.warning('文件数量超出限制');
            return;
        }
        this.fileList.forEach((file: any, index) => {
            formData.append(`doc${nos[index] + 1}`, file);
        });
        formData.append('title', this.saveParam.title);
        formData.append('docType', this.saveParam.docType);
        formData.append('docLevel', this.saveParam.docLevel);
        formData.append('note', this.saveParam.note);
        if (this.mode === 'edit') {
            formData.append('id', this.saveParam.id);
        }
        const saveFunc = {
            'add': this.add,
            'edit': this.update
        }
        saveFunc[this.mode].call(this, formData).subscribe(res => {
            if (res.msg === 'Ok') {
                this.message.success('保存成功');
                this.optSuccess.emit();
                this.cancel();
            } else {
                this.message.success('保存失败');
            }
        });
    }



    cancel() {
        this.optCancel.emit();
    }

    beforeUpload = (file: UploadFile): boolean => {
        this.fileList = this.fileList.concat(file);
        return false;
    };


    /**
     * 新增
     */
    add(param) {
        const dataurl = environment.problem + '/auditProcess/add';
        return this.http.post(dataurl, param);
    }

    /**
     * 修改
     * @param id 1
     */
    update(param) {
        const dataurl = environment.problem + '/auditProcess/update';
        return this.http.post(dataurl, param);
    }

}
